#include "stdafx.h"
#include "widget.h"
#include "profiler.h"
#include "specprivate.h"
#include "iplugin.h"

Widget::Widget(QWidget *parent) 
	: QWidget(parent)
    , tabWidget(new QTabWidget(this))
{
    QVBoxLayout* layout = new QVBoxLayout(this);
    layout->addWidget(tabWidget);
    setLayout(layout);
    setMinimumSize(400, 400);
    loadPlugIn();
}

Widget::~Widget()
{
}

bool Widget::loadPlugIn()
{
	QString file = Profiler::getProfilerFile();
	Profiler profiler;
	if (profiler.read(file)) 
	{
		QList<SpecPrivate*> list = profiler.getSpec();
		foreach (SpecPrivate* spec, list)
		{
			if (spec->enable)
			{
				QPluginLoader pluginLoader(spec->name);
				QObject *plugin = pluginLoader.instance();
				if (plugin) 
				{
					I_PlugIn* iplugin = qobject_cast<I_PlugIn *>(plugin);
					if (iplugin)
					{
						iplugin->initialize();
						QWidget *w = iplugin->getUi();
						if (w)
						{
							tabWidget->addTab(w,iplugin->getName());
						}
					}
				}
			}
		}

		return true;
	}

	return false;
}
